package cn.net.nit.track.service;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import org.apache.log4j.Logger;

import cn.net.nit.track.dao.impl.GPSDaoImpl;
import cn.net.nit.track.entry.GPSEvent;
import cn.net.nit.track.util.GPSTransferUtil;

@Path("/gps")
public class GPSService {

	public static Logger logger = Logger.getLogger(GPSService.class);

	/**
	 * 获取GPS Event 插入一条GPS记录
	 * 测试URL:localhost:8080/track/api/gps
	 * /newEvent?latitude=11&longitude=99&mobileNum=13588026
	 * 
	 * @author hua
	 * @version 1.0 2013-4-21
	 */
	@GET
	@Path("/newEvent")
	public void newEvent(@Context HttpServletRequest httpRequest) {

		GPSEvent event = new GPSEvent();
		event.setLatitude(Double.parseDouble(httpRequest
				.getParameter("latitude")));
		event.setLongitude(Double.parseDouble(httpRequest
				.getParameter("longitude")));
		event.setMobileNum(Long
				.parseLong(httpRequest.getParameter("mobileNum")));
		GPSDaoImpl gpsImpl = new GPSDaoImpl();
		gpsImpl.insertNewGpsLocation(event);
	}

	/**
	 * 根据手机号码，获取当前GPS数据
	 * 测试URL:localhost:8080/track/api/gps/getLocationByMobile?mobileNum=13588026
	 * 
	 * @author hua
	 * @version 1.0 2013-4-21
	 */
	@GET
	@Path("/getLocationByMobile")
	@Produces( { "application/json" })
	public Response getLocationByMobile(@Context HttpServletRequest httpRequest) {

		long mobileNum = Long.parseLong(httpRequest.getParameter("mobileNum"));
		GPSDaoImpl gpsImpl = new GPSDaoImpl();
		GPSEvent event = gpsImpl.getLocationByMobile(mobileNum);
		return Response.status(Response.Status.OK).entity(
				GPSTransferUtil.transferToJson(event).toString()).build();

	}

}
